package com.jason.basicandroid.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class LocalService extends Service {
	private NotificationManager notificationMgr = null;
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		notificationMgr = (NotificationManager)getSystemService(
				NOTIFICATION_SERVICE);
		displayNotificationMessage("Create background service...");
		
		Thread thread = new Thread(null, new ServiceWorker(), "Bacground Service");
		thread.run();
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}
	
	@Override
	public void onDestroy() {
		displayNotificationMessage("Destroy background service...");
		notificationMgr.cancel(R.id.app_notification_id);
		
		super.onDestroy();
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	private void displayNotificationMessage(String msg) {
		Notification notification = new Notification(
				R.drawable.icon, msg, System.currentTimeMillis());
		PendingIntent contentIntent = PendingIntent.getActivity(
				this, 0, new Intent(this, LocalClient.class), 0);
		notification.setLatestEventInfo(
				this, "Background Service", msg, contentIntent);
		notificationMgr.notify(R.id.app_notification_id, notification);
	}
	
	class ServiceWorker implements Runnable {		
		@Override
		public void run() {
			// do something here
		}
	}
}
